package com.awenhui.demo.core.utils;



import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * 图片验证码生成工具
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/10/28 8:57
 */
public class ImageCodeGengerator {

    private int count = 4;
    private int height = 55;
    private String text;
    public BufferedImage image;

    public String getText() {
        return text;
    }

    public BufferedImage getImage() {
        return image;
    }

    /**
     * 设置验证码字符个数和高度，宽度会根据字符个数自动计算
     *
     * @param count  验证码字符个数
     * @param height 验证码图片高度（40）
     */
    public ImageCodeGengerator(int count, int height) {
        this.count = count;
        this.height = height;
        generateImageCode();
    }

    public ImageCodeGengerator() {
        generateImageCode();
    }

    private void generateImageCode() {
        int width = (int) (count * height*0.9);
        int fontHeight= (int) (height*0.8);
        int xStep=(width-30-fontHeight)/(count-1);
        BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
        graphics.setColor(Color.WHITE);//设置画笔颜色-验证码背景色
        graphics.fillRect(0, 0, width, height);//填充背景
        graphics.setFont(new Font("微软雅黑", Font.BOLD,fontHeight));
        //数字和字母的组合
        String baseNumLetter = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

        StringBuffer sBuffer = new StringBuffer();
        int x = 10;  //旋转原点的 x 坐标
        String ch = "";
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            graphics.setColor(getRandomColor());
            //设置字体旋转角度
            int degree = random.nextInt(30);  //角度小于30度
            int dot = random.nextInt(baseNumLetter.length());
            ch = baseNumLetter.charAt(dot) + "";
            sBuffer.append(ch);
            //正向旋转
            graphics.rotate(degree * Math.PI / 180, x, fontHeight);
            graphics.drawString(ch, x, (float) (height*0.65));
            //反向旋转
            graphics.rotate(-degree * Math.PI / 180, x, fontHeight);

            x +=xStep;
        }
        //画干扰线
        for (int i = 0; i < 6; i++) {
            // 设置随机颜色
            graphics.setColor(getRandomColor());
            // 随机画线
            graphics.drawLine(random.nextInt(width), random.nextInt(height),
                    random.nextInt(width), random.nextInt(height));
        }
        //添加噪点
        for (int i = 0; i < 30; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            graphics.setColor(getRandomColor());
            graphics.fillRect(x1, y1, 2, 2);
        }
        this.text = sBuffer.toString();
        this.image = verifyImg;
    }

    /**
     * 输出到流
     *
     * @param os
     * @throws IOException
     */
    public void output(OutputStream os) throws IOException {
        ImageIO.write(image, "png", os);
    }

    /**
     * 输出到文件目录
     *
     * @param path
     * @throws IOException
     */
    public void output(String path) throws IOException {
        ImageIO.write(image, "png", new File(path));
    }

    /**
     * 随机取色
     */
    private  Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256),
                ran.nextInt(256), ran.nextInt(256));
        return color;
    }


}



